package com.tsxy.controller.administrator;

import cn.hutool.core.util.StrUtil;
import com.tsxy.common.APIResult;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tsxy.controller.dto.BookDto;
import com.tsxy.entity.*;
import com.tsxy.mapper.UsedBookPictureRelationMapper;
import com.tsxy.service.IUsedBookPictureRelationService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.tsxy.service.IUsedBookService;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 飞
 * @since 2022-03-12
 */
@RestController
@RequestMapping("/usedBook")
public class UsedBookController {

    @Resource
    private IUsedBookService usedBookService;
    @Resource
    private IUsedBookPictureRelationService usedBookPictureRelationService;
    @Resource
    private UsedBookPictureRelationMapper usedBookPictureRelationMapper;

    //新增或者修改
    @PostMapping
    public APIResult save( @RequestBody UsedBook usedBook){
        return APIResult.success(usedBookService.saveOrUpdate(usedBook)) ;
    }
    @PostMapping("/update")
    public APIResult save2(@RequestBody UsedBook usedBook){
        System.out.println(usedBook);
       // APIResult.success(usedBookService.saveOrUpdate(usedBook))
        return null;
    }

    //新增或者修改
    @PostMapping("/insert")
    @Transactional
    public APIResult insertWithFile(@RequestBody UsedBook book){
        // 先添加book记录
        usedBookService.saveOrUpdate(book);
        //根据file名字查询file的id
        List<Files> files = book.getFilesList();

        List<Integer> ids = files.stream().map(Files::getId).collect(Collectors.toList());
        Map<Integer,String> idName = files.stream().collect( Collectors.toMap(Files::getId, Files::getLocalName) );
        //添加book和file的关系
        if (ids != null && ids.size() > 0){
            int i = usedBookPictureRelationService.insertRelationBatch(book.getId(), ids,idName);
        }else {
            QueryWrapper<UsedBookPictureRelation> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("book_id", book.getId());
            usedBookPictureRelationMapper.delete(queryWrapper);
        }

        return APIResult.success(1);
    }
    //根据id删除
    @DeleteMapping("/{id}")
    public APIResult delete(@PathVariable Integer id) {
        return APIResult.success(usedBookService.removeById(id));
    }
    //批量删除
    @PostMapping("/del/batch")
        public APIResult deleteBath(@RequestBody List<Integer> ids){
        return APIResult.success(usedBookService.removeByIds(ids));
    }
    //查询所有
    @GetMapping
    public APIResult findAll() {
        return APIResult.success(usedBookService.list());
    }
    //根据id查询
    @GetMapping("/{id}")
        public APIResult findOne(@PathVariable Integer id) {
        return APIResult.success(usedBookService.getById(id));
    }
    //分页查询
    @GetMapping("/page")
    public APIResult findPage(@RequestParam Integer pageNum,
                    @RequestParam Integer pageSize) {
        QueryWrapper<UsedBook> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        return APIResult.success(usedBookService.page(new Page<>(pageNum,pageSize), queryWrapper));
    }
    //分页查询
    @GetMapping("/page2")
    public APIResult findPage2(@RequestParam Integer pageNum,
                               @RequestParam Integer pageSize,
                               @RequestParam(defaultValue = "")String name,
                               @RequestParam(defaultValue = "")String author) {
        if ( StrUtil.isBlank(name) ){
            name = null;
        }
        if ( StrUtil.isBlank(author) ){
            author = null;
        }
        List<BookDto> all = usedBookService.getAll(name,author,pageNum,pageSize);
        APIResult apiResult = new APIResult();
        apiResult.setData(all);
        Integer Total = usedBookService.cont();
        apiResult.setTotal(Total);
        apiResult.setStatus("200");
        return apiResult;
    }



}

